package com.zsc.shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.shixun.common.ResultCode;
import com.zsc.shixun.contant.Contant;
import com.zsc.shixun.entity.ArticleCategories;
import com.zsc.shixun.exception.ApiException;
import com.zsc.shixun.mapper.ArticleCategoriesMapper;
import com.zsc.shixun.model.type.TypeCreateVO;
import com.zsc.shixun.model.type.TypeMapping;
import com.zsc.shixun.model.type.TypeUpdateVO;
import com.zsc.shixun.model.type.TypeVO;
import com.zsc.shixun.service.IArticleCategoriesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 文章类型表 服务实现类
 * </p>
 *
 * @author ZWYZY
 * @since 2020-06-06
 */
@Service
@Transactional
public class ArticleCategoriesServiceImpl extends ServiceImpl<ArticleCategoriesMapper, ArticleCategories> implements IArticleCategoriesService {
    private static final Logger logger = LoggerFactory.getLogger(ArticleCategoriesServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TypeMapping typeMapping;

    @Override
    public List<TypeVO> getById(Long id) {
        if (redisTemplate.hasKey(Contant.TYPE_KEY + id)) {
            return (List<TypeVO>) redisTemplate.opsForValue().get(Contant.TYPE_KEY + id);
        }
        List<ArticleCategories> articleCategoriesList = this.list(new QueryWrapper<ArticleCategories>().lambda().eq(ArticleCategories::getUserId, id));
        List<TypeVO> typeVOS = typeMapping.entitysToVos(articleCategoriesList);

        redisTemplate.opsForValue().set(Contant.TYPE_KEY + id, typeVOS);
        return typeVOS;
    }

    @Override
    public void update(TypeUpdateVO typeUpdateVO) {
        ArticleCategories articleCategories = new ArticleCategories();
        BeanUtils.copyProperties(typeUpdateVO, articleCategories);
        try {
            this.updateById(articleCategories);
        } catch (Exception e) {
            logger.error("修改类型失败");
            throw new ApiException(ResultCode.TYPE_UPDATE_FAIL);
        }
        logger.info("修改类型成功");
        if (redisTemplate.hasKey(Contant.TYPE_KEY + articleCategories.getUserId())) {
            redisTemplate.delete(Contant.TYPE_KEY + articleCategories.getUserId());
        }
    }

    @Override
    public void delete(Long id, Long uid) {
        try {
            this.removeById(id);
        } catch (Exception e) {
            logger.error("类型删除失败");
            throw new ApiException(ResultCode.TYPE_DELETE_FAIL);
        }
        logger.info("删除类型成功");
        if (redisTemplate.hasKey(Contant.TYPE_KEY + uid)) {
            redisTemplate.delete(Contant.TYPE_KEY + uid);
        }
    }

    @Override
    public void insert(TypeCreateVO typeCreateVO, Long uid) {
        if(this.getOne(new QueryWrapper<ArticleCategories>().lambda().eq(ArticleCategories::getName,typeCreateVO.getName()))!=null)
        {
            throw new ApiException(ResultCode.TYPE_HAS_EXIST);
        }
        ArticleCategories articleCategories = new ArticleCategories();
        typeCreateVO.setUserId(uid);
        BeanUtils.copyProperties(typeCreateVO, articleCategories);
        try {
            this.save(articleCategories);
        } catch (Exception e) {
            logger.error("创建类型失败");
            throw new ApiException(ResultCode.TYPE_INSERT_FAIL);
        }
        logger.info("创建类型成功");
        if (redisTemplate.hasKey(Contant.TYPE_KEY + uid)) {
            redisTemplate.delete(Contant.TYPE_KEY + uid);
        }
    }

}
